function selectItemType(key, count) {
	document.getElementById('type_id').value = key;
	document.getElementById('submited').value = "selectType";

	if (document.getElementById('submited').value == "selectType")
		document.form.submit();
}

function submitForm() {
	document.getElementById('form').submit();
}

Array.prototype.search = function(search_value) {
	var x = 0; 
	var r = -1;
	
	if(this.length > 0) {
		while(x<this.length) {
			if(String(this[x]).toLowerCase()==String(search_value).toLowerCase()) {
				r = x;
			} x++;
		} 
	}
	
	return r;
};

//Allgemeine Funktion
function addAuthor() {
	if(document.getElementById("addAuthor").value != '') {
		item.addAuthor(document.getElementById("addAuthor").value);
		document.getElementById("addAuthor").value = "";
		document.getElementById("addAuthor").focus();
	}
}

function deleteAuthor(count) {
	item.deleteAuthor(count);
}

function addKeyword() {
	if(document.getElementById("addKeyword").value != '') {
		item.addKeyword(document.getElementById("addKeyword").value);
		document.getElementById("addKeyword").value = "";
		document.getElementById("addKeyword").focus();
	}
}


function deleteKeyword(count) {
	item.deleteKeyword(count);
}


function selectItemType() {
	v = document.getElementById("selectType").value;
	if(v != -1) {
	
		if(v == 1 || v == 2 )	{
		
			document.getElementById("fieldsarea").innerHTML = '<div style="width:90%; clear: both">Add a text field <a href="javascript:addItemField(0);"><img src="/components/com_redrugolok/images/icon_add.png"></a></div>';
			document.getElementById("fieldsarea").focus();
			
		}
		
		if(v == 3 || v == 4 || v == 5) {
		
			document.getElementById("fieldsarea").innerHTML = '<div style="width:90%; clear: both">Add an upload field <a href="javascript:addItemField(1);"><img src="/components/com_redrugolok/images/icon_add.png"></a></div>';
			document.getElementById("fieldsarea").focus();
			
		}
		
		if(v == 6) {
			document.getElementById("fieldsarea").innerHTML = '<div style="width:90%; clear: both">Add a text field <a href="javascript:addItemField(0);"><img src="/components/com_redrugolok/images/icon_add.png"></a></div>';
			document.getElementById("fieldsarea").innerHTML += '<div style="width:90%; clear: both">Add an upload field <a href="javascript:addItemField(1);"><img src="/components/com_redrugolok/images/icon_add.png"></a></div>';
			document.getElementById("fieldsarea").focus();
			
		}
	}
}

function addItemField(type) {
	item.addField(type);
}

function saveItemField(id, obj) {
	item.saveField(id, obj.value);
}

function deleteField(count) {
	item.deleteField(count);
}

function ItemField () {
	this.id = 0;
	this.type = 0; //0 = text, 1 = file
	this.value = "";
}



//Klasse
function Item () {
	this.id = 0;
	this.title = "";
	this.description = "";
	this.authors = new Array();
	this.keywords = new Array();
	this.inspiredBy = "";
	this.showMe = true;
	this.rateMe = true;
	this.fields = new Array();

	this.init = function (){
		
	}
	
	this.setTitle = function (id, value) {
		document.getElementById(id).value = value;
		this.title = value;
	}
	
	this.setDescription = function (id, value) {
		document.getElementById(id).value = value;
		this.description = value;
	}
	
	
	
	
	//AUTHORS
	this.addAuthor = function (value) {
		this.authors = new Array();
		if(value != '') {
			r = this.authors.search(value);
			if(r == -1) {
				this.authors.push(value);
				this.refreshAuthorsArea('last');
			} else {
				this.deleteAuthor(r);
				this.addAuthor(value);
			}
		}
	}

	this.deleteAuthor = function (count) {
		if(this.authors.length > 0) {
			this.authors.splice(count, 1);
			this.refreshAuthorsArea('all');
		}
	}
	
	this.refreshAuthorsArea = function(s) {
		if(s == 'last') {
			count = this.authors.length - 1;
		} else if(s == 'all') {
			count = 0;
			document.getElementById("authorsarea").innerHTML = '';
			}
		
		for(count; count < this.authors.length; count++) {
			document.getElementById("authorsarea").innerHTML += '<li id="authorlist[' + count + ']">'+ this.authors[count] +' <a href="javascript:deleteAuthor('+ count +');"><img src="/components/com_redrugolok/images/icon_delete_small.png"></a></li>';
		}

	}
	
	//KEYWORDS
	this.addKeyword = function (value) {
		if(value != '') {
			valueArr = value.split(' ');
			for(var count = 0; count < valueArr.length; count++) {
				value = valueArr[count];
				r = this.keywords.search(value);
				if(r == -1) {
						this.keywords.push(value);
						this.refreshKeywordsArea('last');
				} else {
					this.deleteKeyword(r);
					this.keywords.push(value);
					this.refreshKeywordsArea('all');
					r = -1;
				}
			}
		}
	}

	this.deleteKeyword = function (count) {
		if(this.keywords.length > 0) {
			this.keywords.splice(count, 1);
			this.refreshKeywordsArea('all');
		}
	}
	
	this.refreshKeywordsArea = function(s) {
		if(s == 'last') {
			count = this.keywords.length - 1;
		} else if(s == 'all') {
				count = 0;
				document.getElementById("keywordsarea").innerHTML = '';
			}
		
		for(count; count < this.keywords.length; count++) {
			document.getElementById("keywordsarea").innerHTML += '<li id="keywordlist[' + count + ']">'+ this.keywords[count] +' <a href="javascript:deleteKeyword('+ count +');"><img src="/components/com_redrugolok/images/icon_delete_small.png"></a></li>';
		}
	}
	
	//FIELDS
	this.addField = function (type) {
		field = new ItemField();
		field.type = type;
		this.fields.push(field);
		this.refreshFieldsArea('last');
	}
	
	this.saveField = function (id, value) {
		this.fields[id].value = value; 
	}

	this.deleteField = function (id) { 
		this.fields.splice(id, 1);
		this.refreshFieldsArea('all');
	}

	this.refreshFieldsArea = function(s) {
		if(s == 'last') {
			count = this.fields.length - 1;
		} else if(s == 'all') {
				count = 0;
				document.getElementById("itemarea").innerHTML = '';
			}
		
		document.getElementById("itemarea").innerHTML = '';
		for(var count = 0; count < this.fields.length; count++) {
			//document.getElementById("itemarea").innerHTML += '<input type="hidden" id="fieldistext['+ count +']" value="' + this.fields[count].type + '">';
			//document.getElementById("itemarea").innerHTML += '<div class="fieldname">Subtitle</div><div class="fieldarea"><input type="text" id="fieldsub['+ count +']"></div>';
			document.getElementById("itemarea").innerHTML += '<div class="fieldname"></div><div class="fieldarea"><textarea cols="50" rows="10" id="field['+ count +']" onChange="saveItemField(' + count + ', this)">'+ this.fields[count].value +'</textarea> <a href="javascript:deleteField('+ count +');"><img src="/components/com_redrugolok/images/icon_delete_small.png"></a></div>';
		}
 
	}
	
	this.generateForm = function() {
		alert(this.authors);
		
	} 
}
